<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
class FLRecordationAndTransferTaxCalculator{
    private $rateInfoProperties;
    private $titleInsuranceCalculatorHelper;
    private $state;
    private $countyLocales;
    private $taxCalculatorHelper;
    private $feeCalculator;

    public function FLRecordationAndTransferTaxCalculator($state, $rateInfoProperties) 
    {
       $this->state = $state;
       $this->rateInfoProperties = $rateInfoProperties;
       
       $this->titleInsuranceCalculatorHelper = new TitleInsuranceCalculatorHelper();
       
       $this->taxCalculatorHelper = new TaxCalculatorHelper();
       
       $this->countyLocales = CountyLocale::getCountyLocales($state);
       $this->feeCalculator = new FeeCalculator($state, $rateInfoProperties);
    }    

    public function supportsCountyLocale() {
        return true;
    }

    public function getCountyLocales() {
        return $this->countyLocales;
    }

    public function supportsTownLocale() {
        return false;
    }

    public function getTownLocales($countyLocale) {
        return array();
    }
    public function calculatePurchaseRecordationAndTransferTax($countyLocale,$townLocale,$consideration,$loanAmounts,$qualifiedFirstTimeBuyer,$ownerOccupiedResidence,$requestedPayer) 
    {

        $loanAmount = $this->taxCalculatorHelper->getLoanTotal($loanAmounts);

        // zero indicates false
        $mortgage = $this->taxCalculatorHelper->getNumberOfLoans($loanAmounts);

        $fees = $this->feeCalculator->calculate(true, $mortgage, $ownerOccupiedResidence);

        // Note it seems that for FL the state/county/recordation categories aren't quite right.  The fees all seem more
        // general yet one differs per county so I put that one in that variable...but all could have been in any, it seems.
        $stateTax = $this->getPurchaseDocumentaryTax($consideration, $countyLocale, $ownerOccupiedResidence);
        $countyTax = null;
        $recordationTax = null;

        if ($loanAmount > 0) {
            $countyTax = $this->getMortgageDocumentaryTax($loanAmount);
            $recordationTax = $this->getMortgageIntangiblesTax($loanAmount);
        }

        // Adam: I don't have an issue with you combining the taxes for the surcharge. However, you should show the deed tax
        // before the mortgage tax, and always show the intangible tax (which is also a mortgage tax and thus charged to buyer)
        // right underneath the mortgage tax.

        // Order displayed in view: state|county|recordation
        return RecordationAndTransferTaxCreator::createSplitRecordationAndTransferTaxes(
                $this->state,
                $stateTax, $countyTax, $recordationTax,
                0.0,
                $fees,
                $requestedPayer, null);
    }


    public function calculateRefinanceRecordationAndTransferTax($countyLocale, $loanAmounts, $ownerOccupiedResidence,$payoffAmounts,$sameLender) {

        $loanAmount = $this->taxCalculatorHelper->getLoanTotal($loanAmounts);

        // zero indicates false
        $mortgage = $this->taxCalculatorHelper->getNumberOfLoans($loanAmounts);

        return new RecordationAndTransferTaxes(
                null, $this->getMortgageDocumentaryTax($loanAmount), $this->getMortgageIntangiblesTax($loanAmount),
                $this->feeCalculator->calculate(false, $mortgage, $ownerOccupiedResidence),
                null);
    }

    
    // In this context ownerOccupiedResidence means 'single family home'

    private function getPurchaseDocumentaryTax($consideration, $countyLocale, $ownerOccupiedResidence) {

        $hundreds = $this->titleInsuranceCalculatorHelper->roundUp2Hundreds($consideration);

        // CountyLocale.Miami_Dade:
        // Documentary Stamp Tax-Deeds .................... $0.60 per $100 or fraction thereof of consideration
        // Documentary Stamp Tax Mortgages .................... $0.35 per $100 or fraction thereof of amount of debt
        // Documentary Surcharge .................... $0.45 per $100 of fraction thereof (see note below)
        // The documentary surcharge only applies to the conveyance of property other than a single family home. In addition,
        // there is an intangibles tax of $0.002 per $1.00 of the amount of debt financed, rounded to the nearest cent.

        // Others
        // TRANSFER AND FINANCING FEES/TAXES:
        // Documentary Stamp Tax-Deeds	.................... $0.70 per $100 or fraction thereof of consideration
        // Documentary Stamp Tax Mortgages	.................... $0.35 per $100 or fraction thereof of amount of debt
        // In addition, there is an intangibles tax of $0.002 per $1.00 of amount financed. The computation is rounded to the nearest cent.

        $tierCosts = array();
        if ($countyLocale->equals(CountyLocale::Miami_Dade)) {
            $cost = $this->rateInfoProperties->getDoubleValue("recordation.state.fl.miamidade.rate");//0.60;
            if ($ownerOccupiedResidence == null || !$ownerOccupiedResidence.getValue()) {
                $cost += $this->rateInfoProperties->getDoubleValue("recordation.state.fl.miamidade.notowneroccupied.premium");//0.45;
            }
            $tierCosts[] = new TierCost(1234567890, cost);
        } else if ($countyLocale->equals(CountyLocale::All_Others)) {
            $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.state.fl.rate")/*0.70*/);
        }

        return new StatePropertyTransferTax("Documentary Stamp Taxes - Deed", $this->titleInsuranceCalculatorHelper->calculateTieredCost( $hundreds, $tierCosts, 0.0));
    }

    private function getMortgageDocumentaryTax($loanAmount) {

        $hundreds = $this->titleInsuranceCalculatorHelper->roundUp2Hundreds($loanAmount);

        $tierCosts = array();
        $tierCosts[] = new TierCost(1234567890, $this->rateInfoProperties->getDoubleValue("recordation.docstamp.fl.rate")/*0.35*/);

        return new CountyPropertyTransferTax("Documentary Stamp Tax - Mortgage", $this->titleInsuranceCalculatorHelper->calculateTieredCost( $hundreds, $tierCosts, 0.0));
    }

    private function getMortgageIntangiblesTax($loanAmount) {

        // In addition, there is an intangibles tax of $0.002 per $1.00 of amount financed. The computation is rounded to the nearest cent.
        $tax = $this->titleInsuranceCalculatorHelper->round2($loanAmount * $this->rateInfoProperties->getDoubleValue("recordation.intangible.fl.rate")/*0.002*/, 2);

        return new RecordationTax("Intangible Tax", $tax);
    }

    public function getHeaderHTML() {
        //todo: get text
        return "The documentary stamp deed taxes are typically paid by the seller; the mortgage stamp deed taxes is paid by the buyer.";
    }

    public function getFooterHTML() {
        //todo: get text
        return null;
    }
}
?>
